package com.nomito.minimito.service.impl;

import com.nomito.minimito.service.ImUserService;
import com.nomito.minimito.vo.GrouperVo;
import com.nomito.minimito.vo.RespGroupInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nomito.minimito.common.utils.PageUtils;
import com.nomito.minimito.common.utils.Query;

import com.nomito.minimito.dao.ImGroupfriendDao;
import com.nomito.minimito.entity.ImGroupfriendEntity;
import com.nomito.minimito.service.ImGroupfriendService;
import org.springframework.transaction.annotation.Transactional;


@Service("imGroupfriendService")
public class ImGroupfriendServiceImpl extends ServiceImpl<ImGroupfriendDao, ImGroupfriendEntity> implements ImGroupfriendService {

    @Autowired
    ImUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImGroupfriendEntity> page = this.page(
                new Query<ImGroupfriendEntity>().getPage(params),
                new QueryWrapper<ImGroupfriendEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public long getGroupCount(Long userId) {
        return baseMapper.selectCount(userId);
    }

    @Override
    public List<Long> listGroupFriendsId(Long groupId) {
        return baseMapper.listGroupFriendsId(groupId);
    }

    @Override
    public List<RespGroupInfoVo> queryMyGroups(Long userId) {
        return baseMapper.queryMyGroups(userId);
    }

    @Override
    @Transactional
    public boolean joinGrops(Long activityId, Long userId) {
        ImGroupfriendEntity groupfriendEntity = new ImGroupfriendEntity();
        groupfriendEntity.setUserId(userId);
        groupfriendEntity.setGroupId(activityId);
        boolean save = this.save(groupfriendEntity);
        return save;
    }

    @Override
    public List<GrouperVo> queryGroupAllHuman(Long groupId) {
        List<Long> list = baseMapper.queryGroupFriendId(groupId);
        List<GrouperVo> collect =new ArrayList<>();
        GrouperVo vo =null;
        for (Long userId:list) {
            vo = userService.getUserInfo(userId);
            collect.add(vo);
        }
        return collect;
    }

}